package com.xjscrm.console.service.customer.wwCustomer.impl;

import com.github.pagehelper.PageHelper;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.Merchant;
import com.xjscrm.common.entity.MerchatChild;
import com.xjscrm.common.enums.WwGroupTaskTypeEnum;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.common.utils.OrderByUtil;
import com.xjscrm.console.mapper.WwGroupTaskMapper;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.group.WwGroupTaskQuery;
import com.xjscrm.console.param.user.ChildMerchatDto;
import com.xjscrm.console.service.common.user.UserService;
import com.xjscrm.console.service.customer.childUser.ChildMerchatService;
import com.xjscrm.console.service.customer.wwCustomer.WwGroupTaskService;
import com.xjscrm.console.vo.MyPageInfo;
import com.xjscrm.console.vo.WwGroupTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WwGroupTaskServiceImpl implements WwGroupTaskService {

    @Autowired
    private WwGroupTaskMapper wwGroupTaskMapper;


    @Autowired
    private UserService userService;

    @Autowired
    private ChildMerchatService  childMerchatService;

    @Override
    public Object getTaskTypeDropdown(BaseDto baseDto) {
         if(baseDto.getMerId()==null){
             throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
         }
        ArrayList<Map> mapArrayList = new ArrayList<>();
        for (WwGroupTaskTypeEnum wwGroupTaskTypeEnum :WwGroupTaskTypeEnum.values()) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("label",wwGroupTaskTypeEnum.getDesc());
            map.put("value",wwGroupTaskTypeEnum.getValue());
            mapArrayList.add(map);
        }
        return mapArrayList;
    }

    @Override
    public Object getTask(WwGroupTaskQuery wwGroupTaskQuery) {
        if(wwGroupTaskQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        String orderBy = OrderByUtil.getOrderBy(wwGroupTaskQuery.getOrderByField(), wwGroupTaskQuery.isAsc());
        PageHelper.startPage(wwGroupTaskQuery.getCurrent(),wwGroupTaskQuery.getSize(),orderBy);
        List<WwGroupTaskVo> wwGroupTaskVoList=wwGroupTaskMapper.getWwGroupTask(wwGroupTaskQuery);

        if(wwGroupTaskVoList!=null&&!wwGroupTaskVoList.isEmpty()){
            for (WwGroupTaskVo wwGroupTaskVo:wwGroupTaskVoList) {
                if(wwGroupTaskVo.getOperateType()==0){
                    BaseDto baseDto=new BaseDto();
                    baseDto.setMerId(wwGroupTaskVo.getOperateId());
                    Merchant merchant = userService.getById(baseDto);
                    wwGroupTaskVo.setOperateName(merchant.getUserName());
                }

                if(wwGroupTaskVo.getOperateType()==1){
                    ChildMerchatDto childMerchatDto=new ChildMerchatDto();
                    childMerchatDto.setMerId(wwGroupTaskVo.getMerId());
                    childMerchatDto.setId(wwGroupTaskVo.getOperateId());
                    MerchatChild merchatChild = childMerchatService.getById(childMerchatDto);
                    wwGroupTaskVo.setOperateName(merchatChild.getUserName());
                }
            }
        }

        return new MyPageInfo<>(wwGroupTaskVoList);
    }
}
